package com.thrd.taiexhibition.ui;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ViewSwitcher;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.facebook.imagepipeline.request.ImageRequest;
import com.thrd.taiexhibition.R;
import com.thrd.taiexhibition.bean.BeanAD;
import com.thrd.taiexhibition.bean.BeanAdvert;
import com.thrd.taiexhibition.fresco.FrescoImage;
import com.thrd.taiexhibition.fresco.FrescoUtils;
import com.thrd.taiexhibition.utils.Constant;

import java.util.List;

/**
 * @author: ${唐杰}
 * @date: 2021/4/1
 */
public class AdImageSwitcher extends ImageSwitcher implements ViewSwitcher.ViewFactory, View.OnClickListener {
    private int position=0;
    private Handler mHandler=new Handler(Looper.getMainLooper());
    private List<BeanAdvert> mBeanAdverts;

    public AdImageSwitcher(Context context) {
        this(context,null);
    }

    public AdImageSwitcher(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFactory(this);
        setOutAnimation(getContext(), R.anim.ad_out_animation);
        setInAnimation(getContext(), R.anim.ad_in_animation);
        setOnClickListener(this);
    }


    public void setImageUrl(String s1,String s2){
        FrescoImage image = (FrescoImage)this.getNextView();
        FrescoUtils.getInstance().setPrefixProgressLoad(getContext(),image,s1);
        showNext();
        ImagePipeline imagePipeline = Fresco.getImagePipeline();
        ImageRequest imageRequest1 = ImageRequest.fromUri(Constant.IMAGE+s2);
        imagePipeline.prefetchToBitmapCache(imageRequest1, getContext());

    }

    @Override
    public View makeView() {
        FrescoImage mFrescoImage=new FrescoImage(getContext());
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        mFrescoImage.setLayoutParams(lp);
        return mFrescoImage;
    }


    public void setBeanAD(BeanAD mBeanAD){
        if(mBeanAD!=null){
            mBeanAdverts = mBeanAD.getAdvertList();
            if(mBeanAdverts !=null&& mBeanAdverts.size()>0){
                mHandler.removeCallbacks(runnable);
                mHandler.post(runnable);
            }
        }
    }


    public void onStrat(){
        mHandler.removeCallbacks(runnable);
        mHandler.postDelayed(runnable, 10000L);
    }

    public void onStop(){
        mHandler.removeCallbacks(runnable);
    }

    private  BeanAdvert mBeanAdvert;
    private Runnable runnable=new Runnable() {
        @Override
        public void run() {
            if(mBeanAdverts!=null&&mBeanAdverts.size()>0){
                mBeanAdvert  =mBeanAdverts.get(position);
                String s1=mBeanAdverts.get(position).getAdvertImg();
                position++;
                if(position>=mBeanAdverts.size()){
                    position=0;
                }
                String s2=mBeanAdverts.get(position).getAdvertImg();
                if(mBeanAdverts!=null){
                    setImageUrl(s1,s2);
                }

                mHandler.postDelayed(runnable, 10000L);
            }

        }
    };

    @Override
    public void onClick(View v) {
        if(onItemClick!=null){
            onItemClick.onItemClick(mBeanAdvert);
        }
    }

    private OnItemClick onItemClick;

    public OnItemClick getOnItemClick() {
        return onItemClick;
    }

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    public interface OnItemClick{
        public void onItemClick(BeanAdvert mBeanAdvert);
    }
}
